package Api.SwipePay;

import Util.HttpUtil;

import java.io.IOException;
import java.net.URLEncoder;
import java.nio.charset.Charset;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import static Util.Tools.encrypt3DES;
import static Util.Tools.getMD5;

/**
 * 付款码支付
 * https://open.heepay.com/www/index.html#/openDoc?type=menu&id=2022210230231
 */
public class SwipePay {

    public static void main(String[] args) {
        try {
            // 目标URL
            String url = "https://pay.heepay.com/API/Swipe/SubmitService.aspx";
            //需要的MD5密钥，商户后台网关密钥下面第一个
            String key = "651886F030134F848F548A12";
            //需要的3DES密钥，商户后台网关密钥下面第一个
            String deskey = "DA95D1144CE146CB89130E1A";
            //商户编号，（汇付宝商户内码：七位整数数字）
            String agent_id = "1664502";
            // 获取当前14位时间戳
            Date now = new Date();
            // 创建 SimpleDateFormat 对象，指定日期格式
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
            String timestamp = dateFormat.format(now);
            //固定版本号
            String version = "2";
            //	1为微信，2为支付宝,3为苹果支付
            String provider_type = "1";
            //授权码，请求时需要3des加密(参与签名，用加密后的参数)
            String auth_code = "123456789456";
            String agent_bill_id = timestamp;
            String agent_bill_time = timestamp;
            String pay_amt = "0.01";
            String user_ip = "127.127.127.127";
            String goods_num = "1";
            String goods_name = "商品名称";
            String goods_name1 = URLEncoder.encode(goods_name, "GBK");
            String remark = "商户自定义,原样返回,可以为空";
            String remark1 = URLEncoder.encode(remark, "GBK");
            String sign_type = "MD5";

            // 调用 encrypt3DES 方法进行加密
            byte[] gbkBytes = auth_code.getBytes(Charset.forName("GBK"));
            String payment_info = encrypt3DES(gbkBytes, deskey);


            //拼接待加密参数
            StringBuilder builder = new StringBuilder();
            builder.append("agent_bill_id=");
            builder.append(agent_bill_id);
            builder.append("&agent_bill_time=");
            builder.append(agent_bill_time);
            builder.append("&agent_id=");
            builder.append(agent_id);
            //签名时授权码用加密后的数据
            builder.append("&auth_code=");
            builder.append(payment_info);
            builder.append("&pay_amt=");
            builder.append(pay_amt);
            builder.append("&provider_type=");
            builder.append(provider_type);
            builder.append("&key=");
            builder.append(key);


            String Date = builder.toString();
            System.out.println("待签名串：" + Date);

            //MD5加密得出签名结果
            String sign = getMD5(Date);


            // 请求参数，以Map形式表示
            Map<String, String> parameters = new HashMap<String, String>();
            parameters.put("version", version);
            parameters.put("agent_id", agent_id);
            parameters.put("agent_bill_id", agent_bill_id);
            parameters.put("agent_bill_time", agent_bill_time);
            parameters.put("provider_type", provider_type);
            parameters.put("payment_info", payment_info);
            parameters.put("pay_amt", pay_amt);
            parameters.put("user_ip", user_ip);
            parameters.put("goods_name", goods_name1);
            parameters.put("goods_num", goods_num);
            parameters.put("remark", remark1);
            parameters.put("sign_type", sign_type);
            parameters.put("sign", sign);
            //请求时授权码用加密后的数据
            parameters.put("auth_code", payment_info);

            String Response = HttpUtil.sendPost1(url, parameters);
            System.out.println("响应：" + Response);

        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
